package cn.kbblog.bean;

import org.slf4j.Logger;

/**
* ajax数据返回对象
*/
public class CommonResult {

    // 响应业务状态
    private Integer code;

    // 响应消息
    private String message;

    // 响应中的数据
    private Object data;
    
    //总数
    private Integer total;

    public static CommonResult build(Integer code, String message, Object Data,Integer total) {
        return new CommonResult(code, message, Data,total);
    }

    public static CommonResult resultSuccess(Object data,Integer total) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_SUCCESS;
    	result.message = Constant.SUCCESS;
    	result.data = data;
    	result.total=total;
    	return result;
    }

    public static CommonResult resultSuccess() {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_SUCCESS;
    	result.message = Constant.SUCCESS;
    	return result;
    }
    public static CommonResult resultSuccess(String message) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_SUCCESS;
    	result.message = message;
    	return result;
    }
    public static CommonResult resultFail(String message) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_WARN;
    	result.message = message;
    	return result;
    }
    /**
     * 只有在异常为InfoException 异常时才返回异常消息
     */
    public static CommonResult resultFail(Exception e) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_WARN;
    	if(e instanceof InfoException){
    		result.message = e.getMessage();
		}else{
			result.message = Constant.FAIL;
		}
    	return result;
    }
    public static CommonResult resultFail(Exception e,Logger logger) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_WARN;
    	if(e instanceof InfoException){
    		result.message = e.getMessage();
    	}else{
    		logger.error(e.getMessage(), e);
    		result.message = Constant.FAIL;
    	}
    	return result;
    }
    
    public static CommonResult resultFail() {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_WARN;
    	result.message = Constant.FAIL;
    	return result;
    }

    public static CommonResult resultError(String message) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_ERROR;
    	result.message = message;
    	return result;
    }
    /**
     * 只有在异常为InfoException 异常时才返回异常消息
     */
    public static CommonResult resultError(Exception e) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_ERROR;
    	if(e instanceof InfoException){
    		result.message = e.getMessage();
		}else{
			result.message = Constant.ERROR;
		}
    	return result;
    }
    public static CommonResult resultError(Exception e,Logger logger) {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_ERROR;
    	if(e instanceof InfoException){
    		result.message = e.getMessage();
    	}else{
    		logger.error(e.getMessage(), e);
    		result.message = Constant.ERROR;
    	}
    	return result;
    }
    
    public static CommonResult resultError() {
    	CommonResult result = new CommonResult();
    	result.code = Constant.STATUS_CODE_ERROR;
    	result.message = Constant.ERROR;
    	return result;
    }
    
    public CommonResult() {
    		
    }

    public static CommonResult build(Integer code, String message,Integer total) {
        return new CommonResult(code, message, null,total);
    }

    public CommonResult(Integer code, String message, Object data,Integer total) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total=total;
    }



    public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}


}
